package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;


    //体检预约
    @Override
    public Result order(Map map) throws Exception {
        //获取传入的时间
        String telephone = (String) map.get("telephone");
        String orderDate = (String) map.get("orderDate");

        //1.查询orderSetting查询预约的那天设置过预约没有
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));

        if (orderSetting == null) {
            //说明当天不能预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2.判断当前天预约的人数是否已经满了
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (number <= reservations) {
            //说明预约的人数已经满了
            return new Result(false,MessageConstant.ORDER_FULL);
        }


        //3.用电话号码查询，判断用户是否注册
        Member member = memberDao.finByTelephone(telephone);
        if (member!=null){
            //5.说明已经注册过，判断是否重复预约
            Order order = new Order(member.getId(),DateUtils.parseString2Date(orderDate),Integer.parseInt((String) map.get("setmealId")));
            List<Order> list = orderDao.findByCondition(order);
            if (list!=null && list.size()>0){
                //说明用户存在重复注册
                return new Result(false,MessageConstant.HAS_ORDERED);
            }


        }else {
            //4.说明没有进行注册，默认进行注册
            member = new Member();
            member.setName((String) map.get("name"));
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setPhoneNumber((String) map.get("telephone"));
            member.setRegTime(new Date());
            memberDao.add(member);//自动完成会员注册

        }

        //5.预约成功添加预约的信息
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")) );
        orderDao.add(order);

        //6.更新预约人数
        orderSetting.setReservations(reservations+1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);


        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }


    //    根据id查询order数据
    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);

        if (map != null) {
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate", DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
